package com.atguigu.spzx.manager.mapper;
import org.apache.ibatis.annotations.Param;

import com.atguigu.spzx.manager.entity.Category;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface CategoryMapper extends BaseMapper<Category> {

    /**
     * 批量插入
     * @param list
     * @return
     */
    int insertList(@Param("list")List<Category> list);

    /**
     * 根据parentId查询
     * @param parentId
     * @return
     */
    default List<Category> getCategoryListByParentId(Long parentId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        return this.selectList(queryWrapper);

    }

    /**
     * 根据parentId查询
     * @param parentId
     * @return
     */
    default Integer getCategoryCountByParentId(Long parentId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        return this.selectCount(queryWrapper);
    }

    /**
     * 获取category的路径 [1,5,255]
     * 1进来返回[1]
     * 5进来返回[1,5]
     * 255进来返回[1,5,255]
     * @param id
     * @return
     */
    default List<Long> getCategoryPathById(Long id) {
        Category category = this.selectById(id);
        Long parentId = category.getParentId();
        if (parentId != 0) {
            List<Long> categoryPath = getCategoryPathById(parentId);
            categoryPath.add(id);
            return categoryPath;
        }else {
            List<Long> categoryPath = new ArrayList<>();
            categoryPath.add(id);
            return categoryPath;
        }
    }
}
